/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ass.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author Thanh
 */
public class OrderDAL {
    public static int getOrderId() throws SQLException, ClassNotFoundException{
        Connection con =null;
        ResultSet rs=null;
        int orderId;
        PreparedStatement psmt = null;
        String sql = "select top(1) * from Orders order by OrderID DESC";
        try {
            con = ConnectionUtitility.openConnection();
            psmt = con.prepareStatement(sql);
            rs=psmt.executeQuery();
            rs.next();
            orderId = rs.getInt("OrderID");
        } finally {
            try {
                psmt.close();
                con.close();
            } catch (Exception e) {
            }
        }
        return orderId;
    }
    public static void insert(String username, String productId, String status, String note, float total, float discountLevel, int quantity, Date date) throws ClassNotFoundException, SQLException {
        Connection con = null;
        PreparedStatement psmt1 = null;
        String sql1 = "insert into Orders(UserName, Note)" + "values(?,?)";
        PreparedStatement psmt2 = null;
        String sql2 = "insert into OrderDetails(OrderID, ProductID, Quantity, DiscountLevel, "
                + "Total, Status, OrderDate, Note) "+"values(?,?,?,?,?,?,?,?)";
        try {
            con = ConnectionUtitility.openConnection();
            psmt1 = con.prepareStatement(sql1);
            psmt1.setString(1, username);
            psmt1.setString(2, productId);
            psmt1.executeUpdate();
        } finally {
            try {
                psmt1.close();
                con.close();
            } catch (Exception e) {
            }
        }
        int orderId=getOrderId();
        try {
            con = ConnectionUtitility.openConnection();
            psmt2 = con.prepareStatement(sql2);
            psmt2.setInt(1, orderId);
            psmt2.setString(2, productId);
            psmt2.setInt(3, quantity);
            psmt2.setFloat(4, discountLevel);
            psmt2.setFloat(5, total);
            psmt2.setString(6, status);
            psmt2.setDate(7, date);
            psmt2.setString(8, note);
            psmt2.executeUpdate();
        } finally {
            try {
                psmt2.close();
                con.close();
            } catch (Exception e) {
            }
        }
    }
      public static boolean checkExistOrder(String productId, Collection<Order> list) {
        if (!list.isEmpty()) {
            Iterator<Order> iter = list.iterator();
            while (iter.hasNext()) {
                Order entity = iter.next();
                if (entity.getProductId().equals(productId)) {
                    Order newen = new Order(entity.getProductId(), entity.getQuantity() + 1);
                    list.remove(entity);
                    list.add(newen);
                    return true;
                }
            }
        }
        return false;
    }

    public static Order searchOrder(String productId, Collection<Order> list) {
        Order order = null;
        if (!list.isEmpty()) {
            Iterator<Order> iter = list.iterator();
            while (iter.hasNext()) {
                Order entity = iter.next();
                if (entity.getProductId().equals(productId)) {
                    order = entity;
                }
            }
        }
        return order;
    }
}
